<template>
  <div class="practiceConfig">
    <el-form
      ref="baseFormRef"
      :model="baseForm"
      :rules="rules"
      label-width="140px"
    >
      <el-row class="mt-20">
        <el-col :span="18">
          <el-form-item label="是否按题库目录答题" prop="practiceQuestionRule">
            <!-- active-开 inactive-关 -->
            <el-switch
              v-model="baseForm.practiceQuestionRule"
              :active-value="1"
              :inactive-value="0"
              active-text="是"
              inactive-text="否"
            />
          </el-form-item>
          <el-form-item label="答题时长" prop="practiceTimes">
            <el-input-number
              v-model="baseForm.practiceTimes"
              :min="1"
              :precision="0"
              :step="1"
              controls-position="right"
            />分钟
          </el-form-item>
          <el-form-item label="总题数" prop="practiceQuestions">
            <el-input-number
              v-model="baseForm.practiceQuestions"
              :min="1"
              :precision="0"
              :step="1"
              controls-position="right"
            />题
            <div class="tip">考试过程中每题分值：试卷总分/总题数</div>
          </el-form-item>
          <el-form-item label="试卷总分" prop="practiceTotalScore">
            <el-input-number
              v-model="baseForm.practiceTotalScore"
              :min="1"
              :precision="0"
              :step="1"
              controls-position="right"
            />分
          </el-form-item>
          <el-form-item label="及格分" prop="practicePassScore">
            <el-input-number
              v-model="baseForm.practicePassScore"
              :min="1"
              :precision="0"
              :step="1"
              controls-position="right"
            />分
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              size="small"
              class="saveBtn"
              :loading="btnLoading"
              @click="submitForm('managePracticeConfig')"
              >保存</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { updateSystemConfig } from "@/api/system/sysConfig";
export default {
  name: "managePracticeConfigSimulation",
  props: {
    dataForm: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      btnLoading: false,
      baseForm: {
        practiceQuestionRule: 0,
        practiceTimes: 1,
        practiceQuestions: 1,
        practiceTotalScore: 1,
        practicePassScore: 10,
      },

      rules: {
        practiceTimes: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        practiceQuestions: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        practiceTotalScore: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        practicePassScore: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    dataForm() {
      this.baseForm = this.dataForm ? this.dataForm : this.baseForm;
    },
  },
  computed: {
    managePracticeConfig() {
      return {
        category: "managePracticeConfig",
        list: [
          // 每日答题配置
          {
            name: "是否按题库目录答题",
            key: "everydayQuestionRule",
            value: this.baseForm.everydayQuestionRule,
          },
          {
            name: "每组题目数",
            key: "everydayQuestions",
            value: this.baseForm.everydayQuestions,
          },
          {
            name: "默认答对积分数密码",
            key: "everydayPassIntegral",
            value: this.baseForm.everydayPassIntegral,
          },
          {
            name: "积分上限",
            key: "everydayMaxIntegral",
            value: this.baseForm.everydayMaxIntegral,
          },
          // 兴趣答题配置
          {
            name: "抽题规则",
            key: "interestQuestionRule",
            value: this.baseForm.interestQuestionRule,
          },
          {
            name: "开放标签",
            key: "interestLabels",
            value: JSON.stringify(this.baseForm.interestLabels),
          },
          {
            name: "每组题目数",
            key: "interestQuestions",
            value: this.baseForm.interestQuestions,
          },
          {
            name: "答对积分数",
            key: "interestPassIntegral",
            value: this.baseForm.interestPassIntegral,
          },
          {
            name: "积分上限",
            key: "interestMaxIntegral",
            value: this.baseForm.interestMaxIntegral,
          },
          // 模拟/自主练习设置
          {
            name: "是否按题库目录答题",
            key: "practiceQuestionRule",
            value: this.baseForm.practiceQuestionRule,
          },
          {
            name: "答题时长",
            key: "practiceTimes",
            value: this.baseForm.practiceTimes,
          },
          {
            name: "总题数",
            key: "practiceQuestions",
            value: this.baseForm.practiceQuestions,
          },
          {
            name: "试卷总分",
            key: "practiceTotalScore",
            value: this.baseForm.practiceTotalScore,
          },
          {
            name: "及格分",
            key: "practicePassScore",
            value: this.baseForm.practicePassScore,
          },
        ],
      };
    },
  },
  methods: {
    /**保存 */
    submitForm(category) {
      this.btnLoading = true;
      console.log("category", category);
      // 安全设置
      if (category == "managePracticeConfig") {
        updateSystemConfig(this.managePracticeConfig)
          .then((res) => {
            this.$message({
              message: res.msg,
              type: "success",
              duration: 1500,
              onClose: () => {
                this.btnLoading = false;
              },
            });
          })
          .catch(() => {
            this.btnLoading = false;
          });
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.practiceConfig {
  padding: 0;

  .saveBtn {
    width: 100px;
  }

  .tip {
    font-size: 14px;
    color: #a5a5a5;
  }

  .lockTime {
    line-height: 32px;
    padding-right: 12px;
    color: #606266;
    font-size: 14px;
  }
}
</style>
